package edu.nf.controller;

import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.nf.dto.EmpAndDepartmDto;
import edu.nf.dto.ResultDto;
import edu.nf.exception.QueryException;
import edu.nf.exception.UpdateException;
import edu.nf.service.*;
import edu.nf.utils.UUIDUtil;
import edu.nf.web.ViewResult;
import edu.nf.web.annotation.RequestMapping;
import edu.nf.web.view.JsonView;

import java.util.List;
import java.util.Map;

@Component("empController")
public class EmpController {
    @Inject(beanName = "findEmpListService")
    private FindAllEmpService service;

    @Inject(beanName = "deleteEmpService")
    private DeleteEmpService deleteEmpService;

    @Inject(beanName = "addEmpService")
    private AddEmpService addEmpService;

    @Inject(beanName = "findCompleteStudentInfoByIdService")
    private FindCompleteEmpByIdService findCompleteEmpByIdService;

    @Inject(beanName = "updateEmpService")
    private UpdateEmpService updateEmpService;

    @Inject(beanName = "matchDepartmService")
    private MatchDepartmService matchDepartmService;


    @RequestMapping(urlPattern = "/findAllEmp")
    public ViewResult findAllEmps(){
        ResultDto dto = new ResultDto();
        try {
            List<Map<String, Object>> students = service.findEmpList();
            dto.setData(students);
        }catch (QueryException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

    @RequestMapping(urlPattern = "/removeEmpInfo")
    public ViewResult removeEmpInfo(String empid){
        ResultDto dto = new ResultDto();
        try{
            deleteEmpService.deleteEmpById(empid);
            dto.setMessage("delete success");
        }catch (UpdateException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

    @RequestMapping(urlPattern = "/addEmpInfo")
    public ViewResult addEmpInfo(EmpAndDepartmDto dto){
        dto.setDepartmId(UUIDUtil.getUUID());
        ResultDto result = new ResultDto();
        try {
            addEmpService.addEmp(dto);
            result.setMessage("add success");
        } catch (UpdateException e) {
            result.setCode(500);
            result.setMessage(e.getMessage());
        }
        return new JsonView(result);
    }

    @RequestMapping(urlPattern = "/findCompleteEmpInfoById")
    public ViewResult findCompleteEmpInfoById(String empid){
        ResultDto dto = new ResultDto();
        try{
            dto.setData(findCompleteEmpByIdService.findCompleteEmpByEmpid(empid));
            dto.setMessage("find complete Emp infomation success");
        }catch (QueryException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

    @RequestMapping(urlPattern = "/updateEmpInfo")
    public ViewResult updateEmpInfo(EmpAndDepartmDto dto){
        ResultDto result = new ResultDto();
        try{
            updateEmpService.updateEmpInfo(dto);

            result.setMessage("alter success");
        }catch (UpdateException e){
            result.setMessage(e.getMessage());
            result.setCode(500);
        }
        return null;
    }

    @RequestMapping(urlPattern = "/matchDepartm")
    public ViewResult matchDepartm(String departm){
        ResultDto dto = new ResultDto();
        try{
            dto.setData(matchDepartmService.matchDepartmById(departm));
        }catch (QueryException e){
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }
}
